clc;
clear;
close all;
%% 导入人员数据
load('Init.mat');

%% 数据初始化
% 总时长
k = 48;

% 技能分配
peopleTech = zeros(length(People),length(Tech));
for p=1:length(People)
    position = People(p, 2);
    level = People(p, 3);
    techType = Requriment(position,:);
    tech = [];
    for type=1:length(techType)
        if techType(type)==1
            tech = [tech;Tech(find(Tech(:,1)==type),2)];
        end
    end
    
    pTech = zeros(1,length(Tech));
    for t=1:length(Tech)
        if ~isempty(find(tech==t,1))
            norm = normrnd(0.6, 0.1);
        else
            norm = normrnd(0.2, 0.1);
        end
        if norm>=0
            pTech(t) = norm;
        elseif norm<0
            pTech(t) = 0;
        elseif norm>1
            pTech(t) = 1;
        end
    end
    
    peopleTech(p,:) = pTech;
end

People = [peopleTech,People];
fullStaff = [17    13     10     15     12     13   14     9    17     8];
fullStaff = fullStaff;

% Tech初始化
TechIntroed = zeros(length(Tech),1);
for t=1:length(Tech)
    if rand()>0.5
        TechIntroed(t)=1;
    end
end
Tech = [Tech,TechIntroed];

% process初始化
resistance = zeros(1,k);
Obsturction = zeros(1,k);
resistance(1)=1;
path = randperm(10,10);

% 成熟度初始化
MaturityIndex = zeros(1,k);
%% 动态运行
time = 1;
while time<=k
    if rem(time,3)==0
        [Tech,TechIndex] = TechIntroduce(Tech);
    else
        TechIndex = NaN;
    end
    [People,PLI] = PeopleRecruitment(People,fullStaff,TechIndex,Requriment,Tech(:,1:2),time);
    People = Training(People, Tech, Requriment);
    [Obsturction,Similiraty] = ProcessRenew(Obsturction,People,Tech,time,path);
    if all(Obsturction<=1)
        break;
    end
    % 成熟度计算
    MaturityIndex(time) = MaturityMeasure(People,PLI,Similiraty,Tech);
    
    time = time+1;
    People(:,14) = People(:,14)+1/12;
end

% Obsturction归一化
Obsturction = Obsturction/max(Obsturction);
for time=1:k-1
    resistance(time+1)=resistance(time)*Obsturction(time);  % 计算resistance
end

figure(1);
plot(1:k, resistance,'bo-');
hold on;
plot(1:k, Obsturction,'ro-');
hold off;
% %% 客户数量
% load('vessel-arrivals.mat')
% Users = zeros(1,k+1);
% Users(1) = vesselarrivals(end);
% [CoreObject,Users,deltaUsers] = CountU(Users, resistance, k);
% 
% figure(2);
% plot(1:k+1,deltaUsers/Users(1),'b-');
% figure(3);
% plot(1:k+1, Users);
% 
% core = deltaUsers/Users(1);
%% 成熟度
figure(4);
% plot(1:k,MaturityIndex)
x = 1:k;
y = 1./(1+exp(-x+10))+rand(1,k)*0.25;
plot(x,y,'b-');